package com.codefish.framework.spring.context.annotation;

import com.codefish.framework.spring.beans.factory.support.BeanDefinition;
import com.codefish.framework.spring.context.stereotype.Component;
import com.codefish.framework.spring.core.utils.AssertUtils;
import com.codefish.framework.spring.core.utils.BeanUtils;
import com.codefish.framework.spring.core.utils.StringUtils;

/**
 * 根据{@link com.codefish.framework.spring.context.stereotype.Component}注解生成beanName
 *
 * @author <a href='https://gitee.com/hzf2281'>codefish</a>
 * @version 1.0.0
 * @since 2022/11/05 下午 01:55
 */
public class AnnotationBeanNameGenerator implements IBeanNameGenerator {
    public static final AnnotationBeanNameGenerator INSTANCE = new AnnotationBeanNameGenerator();

    @Override
    public String generateBeanName(BeanDefinition beanDefinition) {
        AssertUtils.notNull(beanDefinition, "beanDefinition不能为null");
        Class<?> beanClass = beanDefinition.getBeanClass();
        AssertUtils.notNull(beanClass, "beanDefinition的beanClass不能为null");
        String beanName = null;
        Component component = beanClass.getAnnotation(Component.class);
        if (component != null) {
            //如果包含Component注解，则从注解中获取配置的beanName
            beanName = component.value();
        }
        if (StringUtils.isEmpty(beanName)) {
            //beanName为空，则通过class对象生成beanName
            beanName = BeanUtils.generateBeanName(beanClass);
        }
        return beanName;
    }

}
